package com.bootdo.mall_part.service.impl;

import com.bootdo.common.utils.MD5Utils;
import com.bootdo.common.utils.R;
import com.bootdo.lvyou.dao.LvyouCityDao;
import com.bootdo.lvyou.domain.LvyouCity;
import com.bootdo.system.dao.UserDao;
import com.bootdo.system.dao.UserRoleDao;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.domain.UserRoleDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.mall_part.dao.MallHousePlotDao;
import com.bootdo.mall_part.domain.MallHousePlot;
import com.bootdo.mall_part.service.MallHousePlotService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class MallHousePlotServiceImpl implements MallHousePlotService {
	@Autowired
	private MallHousePlotDao mallHousePlotDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private UserRoleDao userRoleDao;

	@Autowired
	private LvyouCityDao lvyouCityDao;
	
	@Override
	public MallHousePlot get(Long id){
		return mallHousePlotDao.get(id);
	}
	
	@Override
	public List<MallHousePlot> list(Map<String, Object> map){
		return mallHousePlotDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return mallHousePlotDao.count(map);
	}

	@Transactional
	@Override
	public int save(MallHousePlot mallHousePlot){
	
		if(mallHousePlot.getId() != null) {
			int result = 0;

			Map<String, Object> param = new HashMap<>();
			if (mallHousePlot.getHouseFlag() != null) {

				if (mallHousePlot.getHouseFlag() == 1) {
					param.put("houseFlag", mallHousePlot.getHouseFlag());
					param.put("communityName", mallHousePlot.getName());
					param.put("communityId", mallHousePlot.getId());
					userDao.updateUser(param);

				} else if (mallHousePlot.getHouseFlag() == 2) {
					param.put("houseFlag", mallHousePlot.getHouseFlag());
					param.put("merchantName", mallHousePlot.getName());
					param.put("merchantId", mallHousePlot.getId());
					userDao.updateUser(param);

				} else {
					return 0;
				}

			} else {
				return 0;
			}
			result = mallHousePlotDao.update(mallHousePlot);
			return result;
		}else {
			return mallHousePlotDao.save(mallHousePlot);
		}		
	
		
	}
	
	@Override
	public int update(MallHousePlot mallHousePlot){
		return mallHousePlotDao.update(mallHousePlot);
	}
	
	@Override
	public int remove(Long id){
		return mallHousePlotDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return mallHousePlotDao.batchRemove(ids);
	}

	@Override
	public List<MallHousePlot> getByCityId(Long cityId) {
		return mallHousePlotDao.getByCityId(cityId);
	}

	@Override
	public List<MallHousePlot> getByCityIdAndName(Long cityId, String name) {
		return mallHousePlotDao.getByCityIdAndName(cityId, name);
	}

	@Override
	public List<MallHousePlot> getByName(String name) {

		return  mallHousePlotDao.getByName(name);
	}

    @Override
    public MallHousePlot getCityDefault(Long cityId) {
        return mallHousePlotDao.getCityDefault(cityId);
    }


    @Transactional
	@Override
	public int saveSelfAndUser(UserDO userDO, MallHousePlot mallHousePlot, boolean houseFlag) {
		int i = 0;

		LvyouCity lvyouCity = lvyouCityDao.get(mallHousePlot.getCityId().intValue());
		mallHousePlot.setCityName(lvyouCity.getName());
		mallHousePlot.setType(2);

		// 用户的账号验证
		if (userDao.getByUsername(userDO.getUsername()) != null) {
			return 0;
		}
		int save = mallHousePlotDao.save(mallHousePlot);

		if (save == 0) {
			return save;
		}

		i = saveUser(userDO, houseFlag, mallHousePlot);

		return i;
	}

	@Override
	public int addManager(MallHousePlot mallHousePlot, UserDO userDO, boolean houseFlag) {
		return saveUser(userDO, houseFlag, mallHousePlot);
	}

	private int saveUser(UserDO userDO, boolean houseFlag, MallHousePlot mallHousePlot) {
		int result = 0;
		if (userDO.getUserId() == null) {
			userDO.setPassword(MD5Utils.encrypt(userDO.getUsername(), userDO.getPassword()));
			if (houseFlag) {
				userDO.setCommunityId(mallHousePlot.getId());
				userDO.setCommunityName(mallHousePlot.getName());
				userDO.setUserType(2);
			} else {
				userDO.setMerchantId(mallHousePlot.getId());
				userDO.setMerchantName(mallHousePlot.getName());
				userDO.setMerchantTypeId(mallHousePlot.getMerchantTypeId());
				userDO.setMerchantTypeName(mallHousePlot.getMerchantTypeName());
				userDO.setUserType(3);
			}

			userDao.save(userDO);
			Long userId = userDO.getUserId();
			List<Long> roles = userDO.getRoleIds();
			userRoleDao.removeByUserId(userId);
			List<UserRoleDO> list = new ArrayList<>();
			for (Long roleId : roles) {
				UserRoleDO ur = new UserRoleDO();
				ur.setUserId(userId);
				ur.setRoleId(roleId);
				list.add(ur);
			}
			if (list.size() > 0) {
				result = userRoleDao.batchSave(list);
			}

		} else {
			result = userDao.update(userDO);
		}

		return result;
	}
}
